import { Injectable } from '@nestjs/common';
import Utils from 'src/utils/Utils';
import * as FormData from 'form-data';
import Constant from 'src/config/Constant';
import Axios from 'src/utils/Axios';

@Injectable()
export class SystemService {
    // 上传 SM.MS 图床
    async uploadImage(base64: string) {
        const buf = Buffer.from(base64, 'base64')
        const form = new FormData()
        form.append('smfile', buf, Utils.guid())

        const header = {
            headers: {
                'Content-Type': 'multipart/form-data',
                'Authorization': Constant.SM_MS_TOKEN,
            }
        }

        const [err, res] = await Axios.axiosPost(`${Constant.SM_MS_URL}/upload`, form, header)
        if (err) throw err.message
        const { success, message } = res
        if (!success) throw message
        return res.data
    }

    // 删除 SM.MS 图床的图片
    async deleteImage(hash: string) {
        const header = {
            headers: {
                'Authorization': Constant.SM_MS_TOKEN,
            }
        }
        const [err, res] = await Axios.axiosGet(`${Constant.SM_MS_URL}/delete/${hash}`, header)
        if (err) throw err.message
        const { success, message } = res
        if (!success) throw message
        return res
    }
}
